﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HitchHikersGuideToTheGalaxy.Models
{
    public class TourDestRepository : ITourDestRepository
    {
        GalaxyDataContext db = new GalaxyDataContext();

        #region Default Add, Delete and save actions

        /// <summary>
        /// Add one tourDest
        /// </summary>
        /// <param name="tourDest">Object TourDest</param>
        public void Add(TourDest tourDest)
        {
            //TODO: finna leið til að adda TourDest
            //db.Tours.InsertOnSubmit(tourDest);
            throw new NotImplementedException();
        }

        /// <summary>
        /// Delete one TourDest object.
        /// </summary>
        /// <param name="tourDest">Object TourDest</param>
        public void Delete(TourDest tourDest)
        {
            //TODO: finna leið til að henda TourDest
            throw new NotImplementedException();
        }

        /// <summary>
        /// Save changes to the database.
        /// </summary>
        public void Save()
        {
            db.SubmitChanges();
        }

        #endregion

        #region Querys

        /// <summary>
        /// Find all TourDest
        /// </summary>
        /// <returns>Returns TourDest object witch include all tour info with destination name and cordinates.</returns>
        public IQueryable<TourDest> FindAllTourDests()
        {
            return (from tour in db.Tours
                    from dest in db.Destinations
                    from dest2 in db.Destinations
                    where tour.DestinationIdTo == dest.Id
                    where tour.DestinationIdFrom == dest2.Id
                    select new TourDest
                    {
                        IdTour = tour.Id,
                        Time = tour.Time,
                        VehicleDriverId = tour.VehicleDriverId,
                        SmokingAllowed = tour.SmokingAllowed,
                        PetAllowed = tour.PetAllowed,
                        AvailableSeats = tour.AvailableSeats,
                        Distance = tour.Distance,
                        Cost = tour.Cost,
                        Route = tour.Route,
                        Note = tour.Note,

                        DestFromName = dest2.Name,
                        DestFromLatitude = dest2.Latitude,
                        DestFromLongitude = dest2.Longitude,

                        DestToName = dest.Name,
                        DestToLatitude = dest.Latitude,
                        DestToLongitude = dest.Longitude
                    }).AsQueryable<TourDest>();
        }

        /// <summary>
        /// Find all upcomming TourDest
        /// </summary>
        /// <returns>TourDest collection.</returns>
        /// 
        public IQueryable<TourDest> FindUpcomingTourDests()
        {
            return (from tour in db.Tours
                    from dest in db.Destinations
                    from dest2 in db.Destinations
                    where tour.DestinationIdTo == dest.Id
                    where tour.DestinationIdFrom == dest2.Id
                    //TODO: Setja time checkið inn eftir test.
                    //where tour.Time > DateTime.Now 
                    //orderby tour.Time
                    select new TourDest
                    {
                        IdTour = tour.Id,
                        Time = tour.Time,
                        VehicleDriverId = tour.VehicleDriverId,
                        SmokingAllowed = tour.SmokingAllowed,
                        PetAllowed = tour.PetAllowed,
                        AvailableSeats = tour.AvailableSeats,
                        Distance = tour.Distance,
                        Cost = tour.Cost,
                        Route = tour.Route,
                        Note = tour.Note,

                        DestFromName = dest2.Name,
                        DestFromLatitude = dest2.Latitude,
                        DestFromLongitude = dest2.Longitude,

                        DestToName = dest.Name,
                        DestToLatitude = dest.Latitude,
                        DestToLongitude = dest.Longitude
                    }).AsQueryable<TourDest>();
        }

        /// <summary>
        /// Find upcoming Tours
        /// </summary>
        /// <returns>Returns TourDest object witch include new tour info with destination name and cordinates.</returns>
        //public IQueryable<Tour> FindUpcomingTours()
        public TourDest GetTourDest(int id)
        {
            return (from tour in db.Tours
                    from dest in db.Destinations
                    from dest2 in db.Destinations
                    where tour.DestinationIdTo == dest.Id
                    where tour.DestinationIdFrom == dest2.Id
                    where tour.Id == id
                    orderby tour.Time
                    select new TourDest
                    {
                        IdTour = tour.Id,
                        Time = tour.Time,
                        VehicleDriverId = tour.VehicleDriverId,
                        SmokingAllowed = tour.SmokingAllowed,
                        PetAllowed = tour.PetAllowed,
                        AvailableSeats = tour.AvailableSeats,
                        Distance = tour.Distance,
                        Cost = tour.Cost,
                        Route = tour.Route,
                        Note = tour.Note,

                        DestFromName = dest2.Name,
                        DestFromLatitude = dest2.Latitude,
                        DestFromLongitude = dest2.Longitude,

                        DestToName = dest.Name,
                        DestToLatitude = dest.Latitude,
                        DestToLongitude = dest.Longitude
                    }).SingleOrDefault<TourDest>();
        }

        /// <summary>
        /// Find all upcoming tours dest with search index.
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        public IQueryable<TourDest> FindUpcomingTourDestsByLocation(String location)
        {
            return (from tour in db.Tours
                    from dest in db.Destinations
                    from dest2 in db.Destinations
                    where tour.DestinationIdTo == dest.Id && dest.Name == location
                    where tour.DestinationIdFrom == dest2.Id && dest.Name == location
                    
                    //TODO: Setja time checkið inn eftir test.
                    where tour.Time > DateTime.Now
                    orderby tour.Time
                    select new TourDest
                    {
                        IdTour = tour.Id,
                        Time = tour.Time,
                        VehicleDriverId = tour.VehicleDriverId,
                        SmokingAllowed = tour.SmokingAllowed,
                        PetAllowed = tour.PetAllowed,
                        AvailableSeats = tour.AvailableSeats,
                        Distance = tour.Distance,
                        Cost = tour.Cost,
                        Route = tour.Route,
                        Note = tour.Note,

                        DestFromName = dest2.Name,
                        DestFromLatitude = dest2.Latitude,
                        DestFromLongitude = dest2.Longitude,

                        DestToName = dest.Name,
                        DestToLatitude = dest.Latitude,
                        DestToLongitude = dest.Longitude
                    }).AsQueryable<TourDest>();
        }

        #endregion
    }
}
